<?php

namespace app\admin\controller;

use think\Request;
use think\Db;
use think\Controller;
use think\Loader;

class Main extends Common {
    /**
     * 后台首页
     */
    public function index(){
        //echo "hello!";
        return $this->fetch();
    }

    public function check_ticket(){
        $ticket_lists = Db::table('ticket_show')->select();
        $ticket_count = Db::table('ticket_show')->count();
        $this->assign("ticket_lists", $ticket_lists);
        $this->assign("ticket_count", $ticket_count);
        return $this->fetch();
    }
    public function start_ticket(){
        //echo "hello!";
        $ticket_lists = Db::table('ticket_show')->select();
        $ticket_count = Db::table('ticket_show')->count();
        $this->assign("ticket_lists", $ticket_lists);
        $this->assign("ticket_count", $ticket_count);
        return $this->fetch();
    }
    public function apply_ticket(){
        //echo "hello!";
        $username = session('username');
        $result = Db::table('ticket_user')->where('name',$username)->find();
        $this->assign("ticket_ret", $result['ticket']);
        $this->assign("ticket_flag", $result['get_ticket']);
        return $this->fetch();
    }

    public function doapply(){
        //if (isset($_POST['username'])) {
        if (request()->isPost()){
            $username = input('post.username');
            $password = input('post.password');

            if (!$username) {
                $this->error('用户名不能为空');
            }
            if (!$password) {
                $this->error('密码不能为空');
            }

            $result = Db::table('ticket_user')->where('name',$username)->find();
            $password = md5(md5($password));
            if($result['password'] != $password){
                $this->error('用户名或密码错误');
            }
            if($result['get_ticket'] != 0){
                $this->error('您已完成申请');
            }
            if($username != session('username')){
                $this->error('用户名或密码错误');
            }

            $ticket_res = Db::table('ticket_show')->find();
            $ticket_num = $ticket_res['last_num'];
            if($ticket_num == 0){
                $this->error('余票为0');
            }
            $ticket_num = $ticket_num - 1;
            $ticket_ret = md5($ticket_res['time'].$ticket_num.$username.rand(1000,9999));

            Db::table('ticket_show')->where('id',1)->update(['last_num'=>$ticket_num]);
            Db::table('ticket_user')->where('name',$username)->update(['get_ticket'=>1, 'ticket'=>$ticket_ret]);
            $this->success('申请成功', 'main/index');
        }
        else{
            $this->error('申请失败');
        }
    }

    public function dostart(){
        //if (isset($_POST['username'])) {
        $ticket_res = Db::table('ticket_show')->find();
        if($ticket_res['is_start'] == 1){
            $this->error('本场演出已经开始放票');
        }
        Db::table('ticket_show')->where('id',1)->update(['is_start'=>1]);
        $this->success('操作成功', 'main/index');
    }


}